import { inject, observer } from 'mobx-react'
import styles from './index.module.less'
import iconSync from '@/resource/archives/icon_sync.png'
import SelectModal from '../selectModal'
import { useState } from 'react'
import store from '../../store'
import { SourceEnum, TrainEnum } from '@/const'
import { api } from '@/servers/api'
import { getData, postData, putData } from '@/servers/http'
import { Dialog } from 'antd-mobile'
import type { ExpectResume } from '../../interface'

const TrainingSync = () => {
    const { fileDetail, getFileDetail, trainInterface, clickOperation } = store || {}
    // 意向-简历列表
    const [resumeObj, setResumeObj] = useState<ExpectResume>()
    // 弹窗显隐
    const [open, setOpen] = useState(false)
    // 弹窗类型 1-意向；2-简历
    const [type, setType] = useState(1)
    // 弹窗列表数据
    const [dataList, setDataList] = useState<any[]>([])
    // 选中的意向数据
    const [selectedIntention, setSelectedIntention] = useState<any>({})

    /** 获取弹窗列表数据 */
    const getDataList = () => {
        getData(api.jobResumeList).then(({ data }) => {
            setResumeObj(data || {})
            setDataList(data.jobExpectList || [])
            setOpen(true)
        })
    }
    /** 选择实训方法 */
    const choiceMethod = () => {
        if (fileDetail.type === TrainEnum.RESUME) {
            getDataList()
        } else {
            trainInterface()
        }
    }
    /** 实训同步 */
    const trainSync = () => {
        Dialog.confirm({
            content: fileDetail.is_finished === '1' ? '档案已完成，是否要重新编辑' : '同步我的实训结果生成档案',
            confirmText: '继续',
            onConfirm: () => {
                choiceMethod()
            },
        })
    }
    /** 同步实训 */
    const batchTrain = async (resumeData: any) => {
        const params = {
            fill_type: 3,
            data: {
                expectCode: selectedIntention?.code,
                resumeCode: resumeData?.code,
                ...selectedIntention,
                ...resumeData,
                updatedAt: String(resumeData.resumeData),
            },
        }
        if (fileDetail?.is_finished === '1') {
            await putData(`${api.userBarriersBatchUpdate}${fileDetail.id}`, params)
        } else {
            await postData(`${api.userBarriersBatchInput}${fileDetail.id}`, params)
        }
        setSelectedIntention({})
        getFileDetail()
    }
    /** 关闭弹窗 */
    const handleCancel = () => {
        setSelectedIntention({})
        setDataList([])
        setType(1)
        setOpen(false)
    }
    /** 确认弹窗 */
    const handleOk = (item: any) => {
        setOpen(false)
        if (type === 1) {
            setSelectedIntention(item || {})
            setDataList(resumeObj?.resumeList || [])
            setType(2)
            setOpen(true)
        } else {
            setDataList([])
            setType(1)
            batchTrain(item)
        }
    }

    return (
        <>
            <div className={styles.header_btn} onClick={() => clickOperation(SourceEnum.TRAIN_SYNC, trainSync)}>
                <img src={iconSync} />
                <span>实训同步</span>
            </div>
            <SelectModal visible={open} type={type} data={dataList} onCancel={handleCancel} onOk={handleOk} />
        </>
    )
}

export default inject('userStore')(observer(TrainingSync))
